package com.ruoyi.project.cyq.item.itask.controller;

import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cyq.client.client.domain.Client;
import com.ruoyi.project.cyq.client.client.service.IClientService;
import com.ruoyi.project.cyq.item.archiving.service.IArchivingService;
import com.ruoyi.project.cyq.item.item.domain.Item;
import com.ruoyi.project.cyq.item.item.domain.ItemVo;
import com.ruoyi.project.cyq.item.item.service.IItemService;
import com.ruoyi.project.cyq.order.domain.Order;
import com.ruoyi.project.cyq.order.domain.OrderProduct;
import com.ruoyi.project.cyq.order.service.IOrderService;
import com.ruoyi.project.cyq.personnelManagement.personnel.domain.Personnel;
import com.ruoyi.project.cyq.personnelManagement.personnel.service.IPersonnelService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.List;

/**
 * @author Strawberry
 * @date 2020-09-18 上午 11:53
 * @desc 快乐将永远依着草莓
 * @desc 项目分配模块的控制器
 */
@Controller
@RequestMapping("/cyq/item/itask")
public class ItaskController extends BaseController {

    private String prefix = "cyq/item/itask";

    @Autowired
    private IItemService itemService;
    /**
     * 员工的service
     */
    @Autowired
    private IPersonnelService personnelService;
    /**
     * 客户的service
     */
    @Autowired
    private IClientService iClientService;
    /**
     * 订单 service
     */
    @Autowired
    private IOrderService iOrderService;
    /**
     * 产品 service
     */
    @Autowired
    private IArchivingService archivingService;


    /**
     * 管理主页的控制器
     */
    @RequiresPermissions("cyq:itask:view")
    @GetMapping()
    public String itemItask() {
        return prefix + "/itask";
    }

    /**
     * 项目的展示
     */
    @RequiresPermissions("cyq:itask:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo showAllItem(ItemVo item) {
//        startPage();
        List<Item> selectAllshow = itemService.selectItemList(item);
        return getDataTable(selectAllshow);
    }

    /**
     * 项目查看具体信息
     * 【项目，员工，客户，订单，产品】
     */
    @RequiresPermissions("cyq:itask:detail")
    @GetMapping("/detail/{projectId}")
    public String showItemDetail(@PathVariable BigInteger projectId, ModelMap modelMap) {
        //        通过项目的id查项目
        ItemVo itemVo = itemService.selectOneById(projectId);
//        通过项目获取 员工的Id查询员工姓名
        Personnel personnel = personnelService.selectPersonnelById(itemVo.getLeaderId());
        itemVo.setLeaderName(personnel.getStaffName());

//        通过项目获取 客户的id查询客户的信息
        Client client = iClientService.selectOneById(itemVo.getClientId());

//        项目==>订单Id==>产品id
        Order order = iOrderService.selectOrderByOrderNumber(Long.valueOf(itemVo.getOrderNumber()));
        OrderProduct orderProduct = null;
        if (order != null) {
            orderProduct = archivingService.selectProjectById(order.getProductId());
        } else {
            orderProduct = new OrderProduct();
            orderProduct.setProductId(-1);
        }

        modelMap.addAttribute("oneItem", itemVo);
        modelMap.addAttribute("client", client);
        modelMap.addAttribute("orderProduct", orderProduct);

        return prefix + "/detail";
    }

    @RequiresPermissions("cyq:itask:edit")
    @GetMapping("/edit/{projectId}")
    public String editArchiving(@PathVariable BigInteger projectId, ModelMap modelMap, Personnel personnel) {
        //        通过项目的id查项目
        ItemVo itemVo = itemService.selectOneById(projectId);
        //        通过项目获取 客户的id查询客户的信息
        Client client = iClientService.selectOneById(itemVo.getClientId());
//        处理所有职员的信息
        List<Personnel> staffList = personnelService.selectPersonnelList(personnel);

        modelMap.addAttribute("oneItem", itemVo);
        modelMap.addAttribute("client", client);
        modelMap.addAttribute("staffList", staffList);

        return prefix + "/edit";
    }

}
